<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Django操作 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/hugoblog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/hugoblog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/hugoblog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/hugoblog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0self ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/hugoblog/"><h1>我的博客</h1></a>
      <p class="lead">
       这里是骆建宇的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/hugoblog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Django操作</h1>
  <time datetime=2020-04-23T12:14:52&#43;0800 class="post-date">Thu, Apr 23, 2020</time>
  <h1 id="导包">导包</h1>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> django.shortcuts <span style="color:#f92672">import</span> render,redirect
<span style="color:#75715e">#导包</span>
<span style="color:#f92672">from</span> django.http <span style="color:#f92672">import</span> HttpResponse,HttpResponseRedirect,JsonResponse
<span style="color:#75715e">#导入类视图</span>
<span style="color:#f92672">from</span> django.views <span style="color:#f92672">import</span> View

<span style="color:#75715e">#from myapp.models import User</span>
<span style="color:#f92672">import</span> json
<span style="color:#f92672">from</span> django.core.serializers <span style="color:#f92672">import</span> serialize
<span style="color:#f92672">from</span> rest_framework.response <span style="color:#f92672">import</span> Response
<span style="color:#f92672">from</span> rest_framework.views <span style="color:#f92672">import</span> APIView
<span style="color:#75715e">#导入加密库</span>
<span style="color:#f92672">import</span> hashlib
<span style="color:#75715e">#导入图片库</span>
<span style="color:#75715e">#绘画库</span>
<span style="color:#f92672">from</span> PIL <span style="color:#f92672">import</span> ImageDraw
<span style="color:#75715e">#字体库</span>
<span style="color:#f92672">from</span> PIL <span style="color:#f92672">import</span> ImageFont
<span style="color:#75715e">#图片库</span>
<span style="color:#f92672">from</span> PIL <span style="color:#f92672">import</span> Image
<span style="color:#75715e">#随机库</span>
<span style="color:#f92672">import</span> random
<span style="color:#75715e">#文件流</span>
<span style="color:#f92672">import</span> io

<span style="color:#f92672">import</span> requests

<span style="color:#75715e">#导入上传文件夹配置</span>
<span style="color:#f92672">from</span> mydjango.settings <span style="color:#f92672">import</span> UPLOAD_ROOT
<span style="color:#f92672">import</span> os

<span style="color:#75715e">#导入原生sql模块</span>
<span style="color:#f92672">from</span> django.db <span style="color:#f92672">import</span> connection

<span style="color:#f92672">import</span> jwt

<span style="color:#75715e">#导入redis数据库</span>
<span style="color:#f92672">import</span> redis

<span style="color:#75715e">#导入时间模块</span>
<span style="color:#f92672">import</span> time

<span style="color:#75715e">#导入公共目录变量</span>
<span style="color:#f92672">from</span> mydjango.settings <span style="color:#f92672">import</span> BASE_DIR

<span style="color:#75715e">#导包</span>
<span style="color:#f92672">from</span> django.db.models <span style="color:#f92672">import</span> Q,F

<span style="color:#75715e">#导入dwebsocket的库</span>
<span style="color:#f92672">from</span> dwebsocket.decorators <span style="color:#f92672">import</span> accept_websocket
<span style="color:#f92672">import</span> uuid

<span style="color:#f92672">from</span> myapp.models <span style="color:#f92672">import</span> User

</code></pre></div><h1 id="md5加密方法">md5加密方法</h1>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">make_password</span>(mypass):

    <span style="color:#75715e"># 生成MD5加密对象</span>
    md5 <span style="color:#f92672">=</span> hashlib<span style="color:#f92672">.</span>md5()

    <span style="color:#75715e"># 转码操作</span>
    mypass_utf8 <span style="color:#f92672">=</span> str(mypass)<span style="color:#f92672">.</span>encode(encoding<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;utf-8&#34;</span>)

    <span style="color:#75715e"># 加密操作</span>
    md5<span style="color:#f92672">.</span>update(mypass_utf8)

    <span style="color:#75715e"># 返回密文</span>
    <span style="color:#66d9ef">return</span> md5<span style="color:#f92672">.</span>hexdigest()

</code></pre></div><h1 id="注册接口-入库">注册接口 入库</h1>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Register</span>(APIView):
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get</span>(self,request):
        <span style="color:#75715e"># 接受参数</span>
        username <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;username&#39;</span>,None)
        password <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;password&#39;</span>,None)
        img <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;img&#39;</span>, None)
        type <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;type&#39;</span>, None)
        phone <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;phone&#39;</span>, None)
        num <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;num&#39;</span>, None)
        <span style="color:#75715e"># 排重操作</span>
        user <span style="color:#f92672">=</span> User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(username<span style="color:#f92672">=</span>username)<span style="color:#f92672">.</span>first()

        <span style="color:#66d9ef">if</span> user:
            <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">403</span>,<span style="color:#e6db74">&#39;message0&#39;</span>:<span style="color:#e6db74">&#39;该用户名已经存在&#39;</span>})

        <span style="color:#75715e"># 入库</span>
        user <span style="color:#f92672">=</span> User(username<span style="color:#f92672">=</span>username,password<span style="color:#f92672">=</span>make_password(password),img<span style="color:#f92672">=</span>img,type<span style="color:#f92672">=</span>type,phone<span style="color:#f92672">=</span>phone,num<span style="color:#f92672">=</span>num)

        <span style="color:#75715e"># 保存结果</span>
        user<span style="color:#f92672">.</span>save()
        <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">200</span>,<span style="color:#e6db74">&#39;message&#39;</span>:<span style="color:#e6db74">&#39;恭喜注册成功&#39;</span>})
</code></pre></div><h1 id="settings配置数据库">settings配置数据库</h1>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">DATABASES <span style="color:#f92672">=</span> {
   <span style="color:#e6db74">&#39;default&#39;</span>: {
          <span style="color:#e6db74">&#39;ENGINE&#39;</span>: <span style="color:#e6db74">&#39;django.db.backends.mysql&#39;</span>, <span style="color:#75715e">#数据库引擎</span>
          <span style="color:#e6db74">&#39;NAME&#39;</span>: <span style="color:#e6db74">&#39;md&#39;</span>,                       <span style="color:#75715e">#数据库名</span>
          <span style="color:#e6db74">&#39;USER&#39;</span>: <span style="color:#e6db74">&#39;root&#39;</span>,                       <span style="color:#75715e">#用户名</span>
          <span style="color:#e6db74">&#39;PASSWORD&#39;</span>: <span style="color:#e6db74">&#39;&#39;</span>,                     <span style="color:#75715e">#密码</span>
          <span style="color:#e6db74">&#39;HOST&#39;</span>: <span style="color:#e6db74">&#39;&#39;</span>,                           <span style="color:#75715e">#数据库主机，默认为localhost</span>
          <span style="color:#e6db74">&#39;PORT&#39;</span>: <span style="color:#e6db74">&#39;&#39;</span>,                           <span style="color:#75715e">#数据库端口，MySQL默认为3306</span>
          <span style="color:#e6db74">&#39;OPTIONS&#39;</span>: {
             <span style="color:#e6db74">&#39;autocommit&#39;</span>: True,
         }
    }
}
</code></pre></div><h1 id="ssettings时区语言">ssettings时区语言</h1>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">LANGUAGE_CODE <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;en-us&#39;</span>

TIME_ZONE <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;Asia/Shanghai&#39;</span>

USE_I18N <span style="color:#f92672">=</span> True

USE_L10N <span style="color:#f92672">=</span> True

USE_TZ <span style="color:#f92672">=</span> False
</code></pre></div><h1 id="配置跨域">配置跨域</h1>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
